package com.tr.rentcar.controller.houtai.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tr.rentcar.model.dao.EmployeeDao;
import com.tr.rentcar.model.dao.OrderDao;
import com.tr.rentcar.model.dao.OrderRecordDao;
import com.tr.rentcar.model.dao.UserDao;
import com.tr.rentcar.model.dao.UserRecordDao;
import com.alibaba.fastjson.JSONArray;
import com.tr.rentcar.model.bean.Admin_he;
import com.tr.rentcar.model.bean.Order;
import com.tr.rentcar.model.bean.User;
import com.tr.rentcar.model.bean.UserRecord;;

@WebServlet("/ordermodify")
public class OrdermodifyControllor extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		OrderDao od=new OrderDao();
		OrderRecordDao ord=new OrderRecordDao();
		
		try {
			//获取当前后台操作人
			Admin_he ah=(Admin_he) req.getSession().getAttribute("admininfo");
			
			int oid=Integer.parseInt(req.getParameter("oid"));
			String ename=req.getParameter("ename");
			String ostatus=req.getParameter("ostatus");
			String orContent=req.getParameter("orContent");
			//修改状态并插入该订单的操作记录
			boolean orstatus=ord.insertRecord(orContent, ah.getAname(), oid);
			if(req.getParameter("uid")==null){
				if("租赁中".equals(ostatus)){
					//修改员工状态
					new EmployeeDao().updateStatus("工作中",ename);
				}else{
					//修改员工状态
					new EmployeeDao().updateStatus("空闲中",ename);
				}
				//更新订单
				resp.getWriter().println(od.updateStatus(ostatus,ename, oid) && orstatus);
			}else{
				//退还押金到账户
				int uid=Integer.parseInt(req.getParameter("uid"));
				double omoney=Double.parseDouble(req.getParameter("omoney"));
				new UserDao().updateMoneyAfterRent(omoney, uid);
				//更新账户消费记录
				UserRecord ur=new UserRecord();
				ur.setuId(uid);
				ur.setUrWay(orContent);
				ur.setUrMoney(omoney);
				ur.setUrdoWay("管理员");
				ur.setUrName(new UserDao().findByid(uid).getUname());
				new UserRecordDao().save(ur);
				//订单减少
				od.updateomoney(omoney, oid);
				resp.getWriter().println(od.updatestatus(ostatus, oid) && orstatus);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}

}
